package com.feng.show.controller;


import com.alibaba.fastjson.JSONObject;
import com.feng.show.comm.ResponseCode;
import com.feng.show.comm.ServerResponse;
import com.feng.show.entity.RfidAgent;
import com.feng.show.service.IAddRfidService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/rfid")
public class AddRfidController {

  @Autowired(required = true)
  IAddRfidService iAddRfidService;


  @PostMapping("/hello")
  public String helloWord() {
    return "你好啊";
  }


    /*
    贴标签，将防伪码导入数据库,将防伪码和产品绑定：
    防伪码列表
    产品列表
    areaId,生产地id
    productId 不同类型的产品的id
    numberList;防伪码编号列表

     */

  @PostMapping("/shipment")
  public ServerResponse<String> shipment(

    @RequestParam(value = "userId") String userId,
    @RequestParam(value = "agentId") String agentId,
    @RequestParam(value = "productId") Integer productId,
    @RequestParam(value = "tids") String tids) {
    userId = "10001";

    if (userId == null || agentId == null || tids == null || productId == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(), "参数错误");

    } else {
      //调用插入数据
      //  DataSecurityCode signData = JSONObject.parseObject(numbers, DataSecurityCode.class);
      List<String> list = JSONObject.parseArray(tids, String.class);
      //  List<String> list=signData.getNumbers();
      if (list == null || list.size() == 0) {
        return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(), "请选择标签");
      }
      System.out.println("list：集合的长度为：--->" + list.size());
      System.out.println("提交的数据为：");
      System.out.println(tids);
      System.out.println("结束：");

      return iAddRfidService.shipment(userId, list, agentId, productId);
    }

  }


  @PostMapping("/getTidInfos")
  public ServerResponse<List<RfidAgent>> getTidInfos(

    @RequestParam(value = "userId") String userId,
    @RequestParam(value = "tids") String tids) {
    userId = "10001";

    if (userId == null || tids == null || tids.isEmpty()) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(), "参数错误");

    } else {
      //调用插入数据
      //  DataSecurityCode signData = JSONObject.parseObject(numbers, DataSecurityCode.class);
      List<String> list = JSONObject.parseArray(tids, String.class);
      //  List<String> list=signData.getNumbers();
      if (list == null || list.size() == 0) {
        return ServerResponse.createBySuccessMsg("请选择标签");
      }
      return iAddRfidService.getRfidListInfoByNos(list, Integer.valueOf(userId));
    }

  }


  @PostMapping("/delRfidList")
  public ServerResponse<String> delRfidList(
    @RequestParam(value = "userId") String userId,
    @RequestParam(value = "tids") String tids) {
    userId = "10001";

    if (userId == null || tids == null || tids.isEmpty()) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(), "参数错误");
    }
    List<String> list;
    try {
      list = JSONObject.parseArray(tids, String.class);
    } catch (Exception e) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(), "参数错误");
    }
    return iAddRfidService.delRfidList(list, Integer.valueOf(userId));

  }






    /*
    删除已经出货的标签，同时把查出的数据，进行保存到logo日志中
     */


//    //根据安全码查询信息
//    @GetMapping ("getAllInfoBySecurtyCode")
//    public ServerResponse<SecurityInfo>  label(
//
//            @RequestParam(value = "userId") String userId,
//            @RequestParam(value = "number") String number)
//
//    {
//        userId="10001";
//
//        if(number==null){
//            return ServerResponse.createByErrorMsg("请输入防伪码");
//
//        }else{
//            //调用插入数据
//            //  DataSecurityCode signData = JSONObject.parseObject(numbers, DataSecurityCode.class);
//
//            //  List<String> list=signData.getNumbers();
////            if(list==null||list.size()==0){
////                return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(),"请选择标签");
////            }
////            return securityCodeServiceIpml.bindSecurityCode(userId,list,productId,areaId);
//
//            return securityCodeServiceIpml.search(userId,number);
//        }
//
//    }
//


}
